package com.chmingx.boot.controller;

import com.chmingx.boot.bean.Person;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ParamTestController {

    // car/2/owner/zhangsan?age=30
    @GetMapping("/car/{id}/owner/{username}")
    public Map<String, Object> getCar(@PathVariable("id") Integer id,
                                      @PathVariable("username") String username,
                                      @PathVariable Map<String, Object> pv,

                                      @RequestHeader("User-Agent") String userAgent,
                                      @RequestHeader Map<String, String> header,

                                      @RequestParam("age") Integer age,
                                      @RequestParam("inters") List<String> inters,
                                      @RequestParam Map<String, String> params,

                                      @CookieValue("_ga") String _ga,
                                      @CookieValue("_ga") Cookie cookie
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("username", username);
        map.put("pv", pv);

        map.put("userAgent", userAgent);
        map.put("header", header);

        map.put("age", age);
        map.put("inters", inters);
        map.put("params", params);

        map.put("_ga", _ga);
        System.out.println(cookie.getName() + "===>" + cookie.getValue());
        return map;
    }

    @PostMapping("/save")
    public Map<String, Object> postMethod(@RequestBody String content) {
        Map<String, Object> map = new HashMap<>();
        map.put("content", content);
        return map;
    }

    // 获取请求中的矩阵变量
    // 访问路径 /car/sell;low=30;brand=bmw,benz,audi
    // springboot 中默认时禁用矩阵变量的，需要手动开启
    // 矩阵变量必须要有url路径变量才能被解析
    @GetMapping("/car/{path}")
    public Map sell(@MatrixVariable("low") Integer low,
                    @MatrixVariable("brand") List<String> brand,
                    @PathVariable("path") String path) {
        Map<String, Object> map = new HashMap<>();
        map.put("low", low);
        map.put("brand", brand);
        map.put("path", path);
        return map;
    }

    // 不同路径下的矩阵变量，需要指明所在路径
    // /boss/1;age=30/2;age=20
    @GetMapping("/boss/{bossId}/{empId}")
    public Map boss(@MatrixVariable(value = "age", pathVar = "bossId") Integer bossAge,
                    @MatrixVariable(value = "age", pathVar = "empId") Integer empAge) {
        Map<String, Object> map = new HashMap<>();
        map.put("bossAge", bossAge);
        map.put("empAge", empAge);
        return map;
    }

    /**
     * 自定义类型参数
     *
     * 页面提交的请求数据GET/POST都可以和对象属性进行绑定
     */
    @PostMapping("/saveuser")
    public Person saveUser(Person person) {
        System.out.println(person);
        return person;
    }
}
